package com.dk.controller;


import com.dk.bean.Result;
import com.dk.em.ResultEnum;
import com.dk.pojo.CarCollection;
import com.dk.service.CarCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张成涛
 * @since 2021-08-13 用户 我的收藏
 */
@RestController
@RequestMapping("/carCollection")
@Api(description = "添加收藏")
public class CarCollectionController {

    @Autowired
    private CarCollectionService carCollectionService;

    @Autowired
    private RedisTemplate redisTemplate;

    /*@PostMapping("toInsert")
    @ApiOperation(value ="我的收藏")
    public Result toInsert(@RequestBody CarCollection carCollection){
        carCollectionService.toInsert(carCollection);
        return Result.buildResult(ResultEnum.SUCCESS);
    }*/


    @ApiOperation(value = "用户我的收藏")
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageId", value = "商品id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "uuid", value = "用户对应的登录态id", required = true, paramType = "query")

    })
    public Result toSelect(@RequestParam("messageId")Long messageId, @RequestParam("uuid")String uuid){
        //首先判断有没有登录
        String str = null;
        if (redisTemplate.hasKey(uuid)){//有就取值
            str = (String)redisTemplate.opsForValue().get(uuid);
        }else {
            return Result.buildResult(ResultEnum.NOLOGIN);
        }
        //增加收藏
        CarCollection carCollection = new CarCollection();
        carCollection.setUserName(str);
        carCollection.setMessageId(messageId);
        return Result.buildResult(ResultEnum.SUCCESS).setData(carCollectionService.toInsert(carCollection));
    }



}

